/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.eschool.dao;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import com.eschool.model.Alert;
import com.eschool.model.Branch;
import com.eschool.model.Permission;
import com.eschool.model.Role;
import com.eschool.model.Room;
import com.eschool.model.SaasAdmin;
import com.eschool.model.School;
import com.eschool.util.HibernateUtil;
import com.eschool.util.JdbcUtil;

/**
 *
 * @author Ammar
 */

public class RoomDao{
	
	
	public static boolean addRoom(Room room) {
    	boolean isInserted = false;
    	
    	Connection connection = JdbcUtil.openConnection();
		try{
			CallableStatement storedProc = connection.prepareCall("{ call InsertRoom(?,?,?,?,?,?) }");
			storedProc.setString(1, room.getRoomTitle());
			storedProc.setInt(2, room.getCapacity());
			storedProc.setBoolean(3, room.getStatus());
			storedProc.setString(4, room.getRoute());
			storedProc.setString(5, room.getDescription());
			storedProc.setInt(6, room.getBranch().getBranchId());

			storedProc.execute();
		
			isInserted = true;
			connection.close();
		}
		
		catch(Exception e){
			isInserted = false;
			connection.close();
			e.printStackTrace();
		}
		finally{
			return isInserted;
		}
		
	}
	
	public static Room getRoomByProperty(String property, Object value) {
		
		Session session = HibernateUtil.currentSession();
	      
		Criteria crit = session.createCriteria(Room.class);
		crit.add(Restrictions.eq(property, value));
		
		Room room = (Room) crit.uniqueResult();
		
        HibernateUtil.closeSession(session);

        return room;
	}
	
	public static List getRooms(Map<String , Object> conditions) {
		
		Session session = HibernateUtil.currentSession();
		Criteria crit = session.createCriteria(Room.class);
		
		if (conditions != null)
			
			for (Iterator iterator = conditions.entrySet().iterator(); iterator.hasNext();) {
			
				Map.Entry condition = (Map.Entry) iterator.next();
				
				crit.add(Restrictions.eq(((String) condition.getKey()), condition.getValue()));
			}
		
		List rooms = (List<Room>) crit.list();
		
        HibernateUtil.closeSession(session);
        
        if(rooms == null)
        	return Collections.EMPTY_LIST;

        return rooms;
	}
	
	public static boolean updateRoom(Room room) {
    	boolean isUpdated = false;
    	
    	Connection connection = JdbcUtil.openConnection();
		try{
			CallableStatement storedProc = connection.prepareCall("{ call spRoomUpdate(?,?,?,?,?,?) }");
			storedProc.setString(1, room.getRoomTitle());
			storedProc.setInt(2, room.getCapacity());
			storedProc.setBoolean(3, room.getStatus());
			storedProc.setString(4, room.getRoute());
			storedProc.setString(5, room.getDescription());
			storedProc.setInt(6, room.getRoomId());

			storedProc.execute();
		
			isUpdated = true;
			connection.close();
		}
		
		catch(Exception e){
			isUpdated = false;
			connection.close();
			e.printStackTrace();
		}
		finally{
			return isUpdated;
		}
		
	}
}
